from LeetCoder import *


class Solution:
    def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
        m, n = len(mat), len(mat[0])
        h = dict()
        for i in range(m):
            for j in range(n):
                v = mat[i][j]
                h[v] = [i, j]
        cm = [0] * m
        cn = [0] * n
        for k, v in enumerate(arr):
            i, j = h[v]
            cm[i] += 1
            cn[j] += 1
            if cm[i] == n or cn[j] == m:
                return k
        return -1


s = Solution()
print(s.firstCompleteIndex(arr=[1, 3, 4, 2], mat=[[1, 4], [2, 3]]))
print(s.firstCompleteIndex(arr=[2, 8, 7, 4, 1, 3, 5, 6, 9], mat=[
      [3, 2, 5], [1, 4, 6], [8, 7, 9]]))
